package com.milhouz.wormdroid.gl.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.skife.csv.CSVReader;
import org.skife.csv.SimpleReader;

import android.util.Log;

import com.milhouz.wormdroid.data.model.WeaponData;
import com.milhouz.wormdroid.gl.model.TargetGLSprite;
import com.milhouz.wormdroid.gl.model.WeaponGLSprite;
import com.milhouz.wormdroid.util.StaticValues;

public class WeaponFactory {
	private static final TargetGLSprite TARGET = new TargetGLSprite();
	private static final WeaponGLSprite[] WEAPONS = new WeaponGLSprite[2];
	
	private static WeaponFactory instance;

	public static WeaponFactory getInstance() {
		if (instance == null) {
			instance = new WeaponFactory();
		}
		return instance;
	}

	public TargetGLSprite getTargetSprite(int botX, int botY) {
		TARGET.initWeapon(botX, botY);
		return TARGET;
	}
	
	@SuppressWarnings("unchecked")
	public WeaponGLSprite[] getWeaponSprites(InputStream inputWeapons) {
		CSVReader csvReader = new SimpleReader();
		csvReader.setSeperator(';');
		try {
			List<String[]> lines = csvReader.parse(inputWeapons);
			if (lines != null) {
				int i = 0;
				for (String[] line : lines){
					WEAPONS[i++] = generateWeaponSpriteFromLine(line);
				}
			}
		} catch (IOException e) {
			Log.e("wormdroid","Error reading weapons file.");
		}
		return WEAPONS;
	}
  
	/*
	 * id; name; type; power; height; width; imgWeapon; imgBullet; needTarget
	 */
	private WeaponGLSprite generateWeaponSpriteFromLine(String[] line) {
		WeaponGLSprite weaponSprite = null;
		if (line != null) {
			WeaponData weaponData = new WeaponData(Integer.valueOf(line[0]), line[1], line[2], Integer.valueOf(line[3]), Integer.valueOf(line[4]), Integer.valueOf(line[5]), Integer.valueOf(line[6]), Integer.valueOf(line[7]));
			weaponSprite = new WeaponGLSprite(StaticValues.spriteId++, 0, 0, weaponData);
		}
		return weaponSprite;
	}
}
